//
//  UIView+CJLongPressHandle.m
//  TSListDemo
//
//  Created by ciyouzen on 2020/3/26.
//  Copyright © 2020 dvlproad. All rights reserved.
//

#import "UIView+CJLongPressHandle.h"
#import <objc/runtime.h>

@interface UIView () {
    
}
@property (nonnull, copy) void(^cjLongPressBeganHandle)(void);  /**< 手势开始要执行的事件 */
@property (nonnull, copy) void(^cjLongPressChangeHandle)(void); /**< 手势变化要执行的事件 */
@property (nonnull, copy) void(^cjLongPressEndHandle)(void);    /**< 手势结束要执行的事件 */

@end

@implementation UIView (CJLongPressHandle)

#pragma mark - runtime【手势GestureRecognizer】
// cjLongPressHandleGR
- (UILongPressGestureRecognizer *)cjLongPressHandleGR {
    return objc_getAssociatedObject(self, @selector(cjLongPressHandleGR));
}

- (void)setCjLongPressHandleGR:(UILongPressGestureRecognizer *)cjLongPressHandleGR {
    objc_setAssociatedObject(self, @selector(cjLongPressHandleGR), cjLongPressHandleGR, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark - runtime【回调Block】
// cjLongPressBeganHandle
- (void (^)(void))cjLongPressBeganHandle {
    return objc_getAssociatedObject(self, @selector(cjLongPressBeganHandle));
}

- (void)setCjLongPressBeganHandle:(void (^)(void))cjLongPressBeganHandle {
    objc_setAssociatedObject(self, @selector(cjLongPressBeganHandle), cjLongPressBeganHandle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

// cjLongPressChangeHandle
- (void (^)(void))cjLongPressChangeHandle {
    return objc_getAssociatedObject(self, @selector(cjLongPressChangeHandle));
}

- (void)setCjLongPressChangeHandle:(void (^)(void))cjLongPressChangeHandle {
    objc_setAssociatedObject(self, @selector(cjLongPressChangeHandle), cjLongPressChangeHandle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

// cjLongPressEndHandle
- (void (^)(void))cjLongPressEndHandle {
    return objc_getAssociatedObject(self, @selector(cjLongPressEndHandle));
}

- (void)setCjLongPressEndHandle:(void (^)(void))cjLongPressEndHandle {
    objc_setAssociatedObject(self, @selector(cjLongPressEndHandle), cjLongPressEndHandle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

#pragma mark - Event
/*
*  添加以Block形式操作手势时机的长按手势（手势有开始、变化、结束等时刻）
*
*  @param beganHandle       手势开始要执行的事件
*  @param changeHandle      手势变化要执行的事件
*  @param endHandle         手势结束要执行的事件
*/
- (void)cj_addLongPressGRWithBeganHandle:(void(^ __nullable)(void))beganHandle
                            changeHandle:(void(^ __nullable)(void))changeHandle
                               endHandle:(void(^ __nullable)(void))endHandle
{
    self.cjLongPressBeganHandle = beganHandle;
    self.cjLongPressChangeHandle = changeHandle;
    self.cjLongPressEndHandle = endHandle;
    
    UILongPressGestureRecognizer *longPressHandleGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(__cjLongPressHandleGRAction:)];
    longPressHandleGR.minimumPressDuration = 0.5;
    [self addGestureRecognizer:longPressHandleGR];
    self.cjLongPressHandleGR = longPressHandleGR;
}

#pragma mark - Private Method
- (void)__cjLongPressHandleGRAction:(UILongPressGestureRecognizer *)longPressGR {
    switch (longPressGR.state) {
        case UIGestureRecognizerStateBegan: {
            !self.cjLongPressBeganHandle ?: self.cjLongPressBeganHandle();
            break;
        }
        case UIGestureRecognizerStateChanged: {
            !self.cjLongPressChangeHandle ?: self.cjLongPressChangeHandle();
            break;
        }
        case UIGestureRecognizerStateEnded: {
            !self.cjLongPressEndHandle ?: self.cjLongPressEndHandle();
            break;
        }
        default:
        {
            break;
        }
    }
}


@end
